﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FrameWork.Output
{
    /// <summary>
    /// 为输出数据提供存储
    /// </summary>
    public static class OutputPool
    {

        #region========私有变量========
        
        private static List<OutputItem> OutputList=new List<OutputItem>();
        private static List<OutputCollectorBase> CollectorList = new List<OutputCollectorBase>();
        
        #endregion

        #region==========事件==========

        public static event OutputCollectorBase.NewOutputGetEventHandler NewOutputGet;

        #endregion

        #region========公有函数========

        /// <summary>
        /// 初始化输出池的所有实例
        /// </summary>
        public static void Init()
        {
            OutputList = new List<OutputItem>();
            CollectorList = new List<OutputCollectorBase>();
        }

        /// <summary>
        /// 获取已注册的输出采集器列表数组
        /// </summary>
        /// <returns></returns>
        public static OutputCollectorBase[] GetCollectors()
        {
            lock (CollectorList)
            {
                return CollectorList.ToArray();
            }
        }

        /// <summary>
        /// 停止全部监控器
        /// </summary>
        public static void Stop()
        {
            foreach(OutputCollectorBase item in GetCollectors())
            {
                if(item.IsStarted)
                    item.Stop();
            }
        }

        /// <summary>
        /// 开始全部监控器
        /// </summary>
        public static void Start()
        {
            foreach(OutputCollectorBase item in GetCollectors())
            {
                if(!item.IsStarted)
                    item.Start();
            }
        }

        /// <summary>
        /// 向输出池注册输出采集器
        /// </summary>
        /// <param name="Collector">要注册的数据采集器</param>
        /// <returns>一个值，表示是否注册成功</returns>
        public static bool RegisterCollector(OutputCollectorBase Collector)
        {
            foreach(OutputCollectorBase item in GetCollectors())
            {
                if(item.GetCollectorType()==Collector.GetCollectorType())
                {
                    return false;
                }
            }
            lock(CollectorList)
            {
                CollectorList.Add(Collector);
                Collector.NewOutputGet += Collector_NewOutputGet;
                return true;
            }
        }

        /// <summary>
        /// 向输出池注册多个输出采集器
        /// </summary>
        /// <param name="Collectors">要注册的数据采集器</param>
        /// <returns>一个值，表示是否全部注册成功</returns>
        public static bool RegisterCollector(IEnumerable<OutputCollectorBase> Collectors)
        {
            bool IsSuc = true;
            foreach(OutputCollectorBase Col in Collectors)
            {
                IsSuc &= RegisterCollector(Col);
            }
            return IsSuc;
        }
       
        #endregion

        #region========私有函数========

        static void Collector_NewOutputGet(object sender, OutputEventArgs e)
        {
            if(NewOutputGet!=null)
            {
                NewOutputGet(sender, e);
            }
        }

        #endregion

    }
}
